<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :inline="true" :model="queryParams">
          <el-form-item label="属性名称" prop="attrName">
            <el-input v-model="queryParams.attrName" clearable placeholder="请输入属性名称" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button v-hasPermi="['pms:attrAttrgroupRelation:add']" icon="Plus" plain type="primary" @click="openSelectUser"
              >添加关联属性
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['pms:attrAttrgroupRelation:remove']"
              :disabled="multiple"
              icon="CircleClose"
              plain
              type="danger"
              @click="cancelLinkAttrAll"
            >
              批量取消关联
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="Close" plain type="warning" @click="handleClose">关闭</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :search="true" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="attrList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="属性名" prop="attrName" />
        <el-table-column align="center" label="是否需要检索" prop="searchType">
          <template #default="scope">
            <dict-tag :options="gulimall_yes_no" :value="scope.row.searchType" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="值类型" prop="valueType">
          <template #default="scope">
            <dict-tag :options="pms_attr_value_type" :value="scope.row.valueType" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="属性图标" prop="icon" />
        <el-table-column align="center" label="可选值列表" prop="valueSelect" />
        <el-table-column align="center" label="属性类型" prop="attrType">
          <template #default="scope">
            <dict-tag :options="pms_attr_attr_type" :value="scope.row.attrType" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="启用状态" prop="enable">
          <template #default="scope">
            <dict-tag :options="gulimall_yes_no" :value="scope.row.enable ? '1' : '0'" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="所属分类" prop="catelogId" />
        <el-table-column align="center" label="快速展示" prop="showDesc">
          <template #default="scope">
            <dict-tag :options="gulimall_yes_no" :value="scope.row.showDesc ? '1' : '0'" />
          </template>
        </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="取消授权" placement="top">
              <el-button
                v-hasPermi="['pms:attrAttrgroupRelation:remove']"
                icon="CircleClose"
                link
                type="primary"
                @click="cancelLinkAttr(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
      <!--      <select-user ref="selectRef" :role-id="queryParams.roleId" @ok="handleQuery" />-->
    </el-card>
  </div>
</template>

<script lang="ts" name="AuthUser" setup>
import SelectUser from './selectUser.vue';
import { RouteLocationNormalized } from 'vue-router';
import { LinkAttrQuery } from '@/api/pms/attrGroup/types';
import { AttrVO } from '@/api/pms/attr/types';
import { listLinkAttr } from '@/api/pms/attrGroup';
import { delAttrAttrgroupRelation } from '@/api/pms/attrAttrgroupRelation';

const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pms_attr_value_type, pms_attr_attr_type, gulimall_yes_no } = toRefs<any>(
  proxy?.useDict('pms_attr_value_type', 'pms_attr_attr_type', 'gulimall_yes_no')
);
const attrList = ref<AttrVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const attrIds = ref<Array<string | number>>([]);

const queryFormRef = ref<ElFormInstance>();
const selectRef = ref<InstanceType<typeof SelectUser>>();

const queryParams = reactive<LinkAttrQuery>({
  attrGroupId: parseInt(route.params.attrGroupId as string),
  attrName: undefined,
  pageNum: 1,
  pageSize: 10
});

/** 查询授权-关联属性列表 */
const getList = async () => {
  loading.value = true;
  const res = await listLinkAttr(queryParams);
  attrList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
// 返回按钮
const handleClose = () => {
  const obj: RouteLocationNormalized = {
    path: '/pms/attrGroup',
    fullPath: '',
    hash: '',
    matched: [],
    meta: undefined,
    name: undefined,
    params: undefined,
    query: undefined,
    redirectedFrom: undefined
  };
  proxy?.$tab.closeOpenPage(obj);
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
// 多选框选中数据
const handleSelectionChange = (selection: AttrVO[]) => {
  attrIds.value = selection.map((item) => item.attrId);
  multiple.value = !selection.length;
};
/** 打开授权-关联属性表弹窗 */
const openSelectUser = () => {
  selectRef.value?.show();
};
/** 取消授权按钮操作 */
const cancelLinkAttr = async (row: AttrVO) => {
  await proxy?.$modal.confirm('确认要取消该-关联属性"' + row.attrName + '"角色吗？');
  await delAttrAttrgroupRelation(row.attrId);
  await getList();
  proxy?.$modal.msgSuccess('取消授权成功');
};
/** 批量取消授权按钮操作 */
const cancelLinkAttrAll = async () => {
  const ids = attrIds.value.join(',');
  await proxy?.$modal.confirm('是否取消选中-关联属性授权数据项?');
  await delAttrAttrgroupRelation(ids);
  await getList();
  proxy?.$modal.msgSuccess('取消授权成功');
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped></style>
